#!/bin/bash

# generate the .h file

KEMI_MAX_SIZE=1024

cat > ../app_sqlang_kemi_export.h <<EOF
/**
 * Copyright (C) 2017 Daniel-Constantin Mierla (asipto.com)
 *
 * This file is part of Kamailio, a free SIP server.
 *
 * Kamailio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version
 *
 * Kamailio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

/**
 * this file is generated - do not edit
 */

#ifndef __APP_SQLANG_KEMI_EXPORT_H__
#define __APP_SQLANG_KEMI_EXPORT_H__

#include <squirrel.h>
#include "../../core/kemi.h"

#define SR_KEMI_SQLANG_EXPORT_SIZE	${KEMI_MAX_SIZE}

typedef struct sr_kemi_sqlang_export {
	SQFUNCTION pfunc;
	sr_kemi_t *ket;
} sr_kemi_sqlang_export_t;

sr_kemi_t *sr_kemi_sqlang_export_get(int idx);
SQFUNCTION sr_kemi_sqlang_export_associate(sr_kemi_t *ket);

#endif
EOF

# generate the .c file

cat > ../app_sqlang_kemi_export.c <<EOF
/**
 * Copyright (C) 2016 Daniel-Constantin Mierla (asipto.com)
 *
 * This file is part of Kamailio, a free SIP server.
 *
 * Kamailio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version
 *
 * Kamailio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

/**
 * this file is generated - do not edit
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

#include "../../core/dprint.h"

#include "app_sqlang_api.h"
#include "app_sqlang_kemi_export.h"

EOF

CEND=${KEMI_MAX_SIZE}

for (( c=0; c<CEND; c++ )); do
	echo >>../app_sqlang_kemi_export.c
	echo "/**" >>../app_sqlang_kemi_export.c
	echo " *" >>../app_sqlang_kemi_export.c
	echo " */" >>../app_sqlang_kemi_export.c
	echo "static SQInteger sr_kemi_sqlang_exec_func_${c}(HSQUIRRELVM J)" >>../app_sqlang_kemi_export.c
	echo "{" >>../app_sqlang_kemi_export.c
	echo "	return sr_kemi_sqlang_exec_func(J, ${c});" >>../app_sqlang_kemi_export.c
	echo "}" >>../app_sqlang_kemi_export.c
done

echo >>../app_sqlang_kemi_export.c
echo "/**" >>../app_sqlang_kemi_export.c
echo " *" >>../app_sqlang_kemi_export.c
echo " */" >>../app_sqlang_kemi_export.c

echo "static sr_kemi_sqlang_export_t _sr_kemi_sqlang_export_list[] = {" >>../app_sqlang_kemi_export.c
for (( c=0; c<CEND; c++ )); do
	echo "	{ sr_kemi_sqlang_exec_func_${c}, NULL}," >>../app_sqlang_kemi_export.c
done
echo "	{NULL, NULL}" >>../app_sqlang_kemi_export.c
echo "};" >>../app_sqlang_kemi_export.c

cat >> ../app_sqlang_kemi_export.c <<EOF

/**
 *
 */
sr_kemi_t *sr_kemi_sqlang_export_get(int idx)
{
	if(idx<0 || idx>=SR_KEMI_SQLANG_EXPORT_SIZE)
		return NULL;
	return _sr_kemi_sqlang_export_list[idx].ket;
}

/**
 *
 */
SQFUNCTION sr_kemi_sqlang_export_associate(sr_kemi_t *ket)
{
	int i;
	for(i=0; i<SR_KEMI_SQLANG_EXPORT_SIZE; i++) {
		if(_sr_kemi_sqlang_export_list[i].ket==NULL) {
			_sr_kemi_sqlang_export_list[i].ket = ket;
			return _sr_kemi_sqlang_export_list[i].pfunc;
		}
		if(_sr_kemi_sqlang_export_list[i].ket==ket) {
			return _sr_kemi_sqlang_export_list[i].pfunc;
		}
	}
	LM_ERR("no more indexing slots\n");
	return NULL;
}
EOF
